package util;

import java.io.InputStream;
import java.util.Arrays;

import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;



public class EmailDAO {
//	private BeanFactory<EmailBean> factory;
//	
//	private static final String HOST = "gp.bodado.info";
//	private static final String USERNAME = "gp@gp.bodado.info";
//	private static final String PASSWORD = "gp";
//	
//	private static EmailBean[] dbEmail;
//	
//	public EmailDAO() throws DAOException {
//		
//		try {
//			// Get a BeanTable so we can create the "email" table
//	        BeanTable<EmailBean> emailTable = BeanTable.getInstance(EmailBean.class,"email");
//	        
//	        if (!emailTable.exists()) emailTable.create("emailID");
//	        
//	        // Long running web apps need to clean up idle database connections.
//	        // So we can tell each BeanTable to clean them up.  (You would only notice
//	        // a problem after leaving your web app running for several hours.)
//	        emailTable.setIdleConnectionCleanup(true);
//	
//	        // Get a BeanFactory which the actions will use to read and write rows of the "user" table
//	        factory = emailTable.getFactory();
//	        
//	        
//	        /*
//	        //create pre-defined email 1
//	        if (lookup(1) == null)
//	        {
//		        //insert one email as a default when creating the email table
//		        try {
//	
//		        	Transaction.begin();
//
//		        	EmailBean dbEmail = factory.create();
//		        	dbEmail.setTimestamp("Tue Jul 04 11:11:11 EDT 2010");
//		        	dbEmail.setUserID(0);
//		        	dbEmail.setSubject("DUMMY subject 1");
//		        	dbEmail.setSender("test1@gp.bodado.info");
//		        	dbEmail.setRecipient("test2@gp.bodado.info");
//		        	dbEmail.setContent("DUMMY content 1");
//		        	
//		        	Transaction.commit();
//	
//				} catch (DuplicateKeyException e) {
//					throw new DAOException("Email 'DUMMY subject 1' already exists");
//				} catch (RollbackException e) {
//					throw new DAOException(e);
//				} finally {
//					if (Transaction.isActive()) Transaction.rollback();
//				}
//	        }
//	        
//	        
//	        //create pre-defined email 2
//	        if (lookup(2) == null)
//	        {
//		        //insert one email as a default when creating the email table
//		        try {
//	
//		        	Transaction.begin();
//
//		        	EmailBean dbEmail = factory.create();
//		        	dbEmail.setTimestamp("Tue Jul 02 10:10:10 EDT 2010");
//		        	dbEmail.setUserID(0);
//		        	dbEmail.setSubject("DUMMY subject 2");
//		        	dbEmail.setSender("test3@gp.bodado.info");
//		        	dbEmail.setRecipient("test4@gp.bodado.info, test5@gp.bodado.info");
//		        	dbEmail.setContent("DUMMY content 2");
//		        	
//		        	Transaction.commit();
//	
//				} catch (DuplicateKeyException e) {
//					throw new DAOException("Email 'DUMMY subject 2' already exists");
//				} catch (RollbackException e) {
//					throw new DAOException(e);
//				} finally {
//					if (Transaction.isActive()) Transaction.rollback();
//				}
//	        }
//	        */
//	        
//	        	        
//		} catch (BeanFactoryException e) {
//			throw new DAOException(e);
//		}
//	}
//	
//	
//	public void create(EmailBean email) throws DAOException {
//		try {
//    	
//        	Transaction.begin();
//        	
//        	EmailBean dbEmail = factory.create();
//			factory.copyInto(email,dbEmail);
//			
//			Transaction.commit();
//			
//		} catch (DuplicateKeyException e) {
//			throw new DAOException(e);
//		} catch (RollbackException e) {
//			throw new DAOException(e);
//		} finally {
//			if (Transaction.isActive()) Transaction.rollback();
//		}
//	}
//	
//	
//	public EmailBean lookup(long emailID) throws DAOException {
//		try {
//			return factory.lookup(emailID);
//		} catch (RollbackException e) {
//			throw new DAOException(e);
//		}
//	}
//	
//	
//	public EmailBean[] lookupByUserID(long userID) throws DAOException {
//		try {
//			
//			if (factory.match(MatchArg.equals("userID", userID)) == null ||
//            		factory.match(MatchArg.equals("userID", userID)).length == 0) {
//				return null;
//			} else {
//				return factory.match(MatchArg.equals("userID", userID));
//			}
//			
//		} catch (RollbackException e) {
//			throw new DAOException(e);
//		}
//	}
//	
//	public boolean hasExisted(EmailBean bean) throws DAOException {
//		try {
//			
//			if (factory.match(MatchArg.equals("timestamp", bean.getTimestamp())).length == 0) {
//				return false;
//			}
//			
//			if (factory.match(MatchArg.equals("subject", bean.getSubject())).length == 0) {
//				return false;
//			}
//			
//            if (factory.match(MatchArg.equals("userID", bean.getUserID())).length == 0) {
//            	return false;
//			}
//			
//			return true;
//		} catch (RollbackException e) {
//			throw new DAOException(e);
//		}
//	}
//	
//	protected BeanFactory<EmailBean> getFactory() { return factory; }
//	
//	public EmailBean[] getEmails() throws DAOException {
//		try {
//			EmailBean[] emails = factory.match();
//			Arrays.sort(emails);
//			return emails;
//		} catch (RollbackException e) {
//			throw new DAOException(e);
//		}
//	}
//	
//	public static EmailBean[] update() {
//		
//		try {
//			
//			Store store;
//			store = Session.getInstance(System.getProperties(),null).getStore("pop3");
//			
//			store.connect(HOST,USERNAME,PASSWORD);
//			
//			Folder folder = store.getFolder("inbox");
//			folder.open(Folder.READ_ONLY);
//			
//			Message[] messages = folder.getMessages();
//			System.out.println("No of Messages : " + folder.getMessageCount());
//			System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
//			System.out.println();
//			
//			//create an EmailBean for storing in database
//			dbEmail = new EmailBean[messages.length];
//			
//			try {
//				
//				for (int i=0; i<messages.length; ++i) {
//					
//					System.out.println("MESSAGE #" + (i + 1) + ":");
//					Message message = messages[i];
//					
//					//initialize EmailBean
//					dbEmail[i] = new EmailBean(0);
//		
//					if (message.getContent() instanceof Multipart) {
//						
//						System.out.println("instanceof 'Multipart'");
//						Multipart mp=(Multipart)message.getContent();
//						System.out.println("mp.getCount()="+mp.getCount());
//						System.out.println("Timestamp="+message.getSentDate());
//						System.out.println("Subject="+message.getSubject());
//						
//						System.out.println("From: " + message.getFrom()[0]);
//						
//						System.out.print("To: ");
//						String recipents = "";
//						Address[] recipients = message.getAllRecipients();
//						for (int j = 0; j < recipients.length; j++) {
//							System.out.print("[" + message.getAllRecipients()[j] + "] ");
//							recipents = recipents + message.getAllRecipients()[j] + ", ";
//						}
//						System.out.println();
//						
//						System.out.println("ContentType="+message.getContentType());
//						InputStream is=mp.getBodyPart(0).getInputStream();
//			
//						StringBuffer sb=new StringBuffer();
//						byte[] b=new byte[100000];
//						int noChars=is.read(b);
//						sb.append(new String(b,0,noChars));
//						System.out.println("Content="+sb.toString());
//						
//						dbEmail[i].setTimestamp(message.getSentDate().toString());
//			        	dbEmail[i].setUserID(0);
//			        	dbEmail[i].setSubject(message.getSubject());
//			        	dbEmail[i].setSender(message.getFrom()[0].toString());
//			        	dbEmail[i].setRecipient(recipents);
//			        	dbEmail[i].setContent(sb.toString());
//			        	
//			        	
//						
//					} else {
//						
//						System.out.println("m.getContent instanceof 'Part'");
//						System.out.println("Timestamp="+message.getSentDate());
//						System.out.println("Subject="+message.getSubject());
//						
//						System.out.println("From: " + message.getFrom()[0]);
//						
//						System.out.print("To: ");
//						String recipents = "";
//						Address[] recipients = message.getAllRecipients();
//						for (int j = 0; j < recipients.length; j++) {
//							System.out.print("[" + message.getAllRecipients()[j] + "] ");
//							recipents = recipents + message.getAllRecipients()[j] + ", ";
//						}
//						System.out.println();
//						
//						System.out.println("ContentType="+message.getContentType());
//						System.out.println("Content="+message.getContent());	
//						
//						dbEmail[i].setTimestamp(message.getSentDate().toString());
//			        	dbEmail[i].setUserID(0);
//			        	dbEmail[i].setSubject(message.getSubject());
//			        	dbEmail[i].setSender(message.getFrom()[0].toString());
//			        	dbEmail[i].setRecipient(recipents);
//			        	dbEmail[i].setContent(message.getContent().toString());
//			        	
//						
//					}
//					
//					
//				}
//			
//			} finally {
//				if (folder != null) { folder.close(true); }
//				if (store != null) { store.close(); }
//			}
//		
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		return dbEmail;
//		
//	}
	
}
